package com.junxonline.plat.dao.repository;

import com.junxonline.plat.dao.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName: UserRepository
 * @Description: 用户数据操作类
 * @author JunX
 */

@Repository
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    User findByUserId(Long userId);

    User findByUsername(String username);

    User findByUsernameAndPassword(String username, String password);

    List<User> findByUserIdIn(Long[] userIds);

    @Modifying
    @Query(value ="DELETE FROM sys_user_position WHERE user_id = ?1 AND position_id = ?2", nativeQuery = true)
    void deleteUserPositionIn(Long userId, Long positionId);

    @Modifying
    @Query(value ="DELETE FROM sys_user_role WHERE user_id = ?1 AND role_id = ?2", nativeQuery = true)
    void deleteUserRoleIn(Long userId, Long roleId);

    @Modifying
    @Query(value = "INSERT INTO sys_user_role (user_id, role_id) VALUES(?1, ?2)", nativeQuery = true)
    void saveUserRoleIn(Long userId, Long roleId);

}
